/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package commerceone;
import com.sun.crypto.provider.RSACipher;
import java.sql.*;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author ahmed
 */
public class bd {
   public  bd (){
   }
   Connection con=null;
 public  void connect(String url,String bdname,String pass)
    { try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      this.con=DriverManager.getConnection(url,bdname,pass);
      } catch(Exception e){System.out.print("mot de passe ou nom de base non valid");} }


public String auto_incremont(String table,String id)
    {int max =0;
        try {
            Statement s = this.con.createStatement();
            ResultSet rs = s.executeQuery("select max(" + id + ") from " + table);
            while (rs.next())
                {
                max= rs.getInt(1);
                }
            s.close();
            //this.con.close();
            
        } catch (Exception e){e.printStackTrace();
        }
    return Integer.toString(max+1);}
public void select(String table,String champ,String condition)
    {if (condition.equals(""))
            {try{
         Statement s=this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
,ResultSet.CONCUR_UPDATABLE);
      ResultSet rs=s.executeQuery("select "+champ+"from "+table);
      //ResultSetTableModel rtm = new ResultSetTableModel( rs );
      //select s1= new select(rtm);
      while (rs.next())
      {
      System.out.print(rs.getInt(1));
      System.out.println(rs.getString(2));
                }
      s.close();
      //this.con.close();
    } catch(Exception e){e.printStackTrace();}}
    else
             {try{
         Statement s=this.con.createStatement();
      ResultSet rs=s.executeQuery("select "+champ+"from "+table+" where "+condition);
      while (rs.next())
      {
      System.out.println(rs.getInt(1));
                }
      s.close();
      //this.con.close();
    } catch(Exception e){e.printStackTrace();}}
    }
public void insert(String Table,String id,String value)
    {try{
         Statement s=this.con.createStatement();
         s.execute("INSERT INTO "+Table+" VALUES"+" ('"+auto_incremont(Table, id)+"',"+value+")");
         s.close();

      //this.con.close();
    } catch(Exception e){e.printStackTrace();}}
}
